package com.ctg.itrdc.k8sconsole.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.K8sAlertDTO;
import com.ctg.itrdc.k8sconsole.service.IAlarmService;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;

/**
 * 
 * @description: k8s告警
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年9月21日 下午4:21:50 Piao.Wu v1.0.0 create
 *
 */
@Controller
@RequestMapping("/alert")
public class AlarmController {

	@Resource
	private IAlarmService alarmService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@RequestMapping(value = "/queryAlerts", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryAlerts(@RequestParam("clusterCode") String clusterCode,
			@RequestParam(value = "id", required = false) Long id, @RequestParam("perpage") String perpage,
			@RequestParam("page") String page, @RequestParam("isactive") Boolean isactive,
			@RequestParam(value = "alertType", required = false) String alertType,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime) {
		if ("0".equals(clusterCode)) {
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, new HashMap<>());
		}
		
		Map<String, Object> map = null;
		String jsonStr = null;

		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = alarmService.getAlarms(clusterCode, id, perpage, page, isactive, alertType, startTime,
						endTime);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("queryAlerts出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}

	@RequestMapping(value = "/queryHeadK8sAlerts", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryHeadK8sAlerts() {
		try {
			Map<String, Object> map = alarmService.queryHeadK8sAlarms();
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/queryAlertTypes", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryAlertTypes(@RequestParam("clusterCode") String clusterCode) {
		List<Map<String, Object>> list = null;
		String jsonStr = null;

		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = alarmService.queryAlertTypes(clusterCode);
				list = JSONUtils.jsonToMapList(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (list == null) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, list);
	}

	@RequestMapping(value = "/queryAlertRules", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryAlertRules(
			@RequestParam("clusterCode") String clusterCode
			,@RequestParam(value = "id", required = false) Long id
			,@RequestParam(value = "alertType", required = false) String alertType
			,@RequestParam(value = "isactive", required = false) Boolean isactive
			,@RequestParam(value = "startTime", required = false) Long startTime
			,@RequestParam(value = "endTime", required = false) Long endTime
			,@RequestParam("perpage") Integer perpage,
			@RequestParam("page") Integer page) {
		Map<String, Object> map = null;
		String jsonStr = null; 
		try {
			jsonStr = alarmService.queryAlertRules(clusterCode, id, alertType, isactive, startTime, endTime, perpage, page);
			map = JSONUtils.jsonToMap(jsonStr);
		} catch (Exception e) {
			logger.error("queryAlertRules查询出错！", e);
			jsonStr = e.getLocalizedMessage();
		}	
		if (map == null) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	@RequestMapping(value = "/queryAlertRule", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryAlertRule(@RequestParam("clusterCode") String clusterCode,
			@RequestParam("id") Long id) {
		Map<String, Object> map = null;
		String jsonStr = null;
		try {
			jsonStr = alarmService.queryAlertRule(clusterCode, id);
			map = JSONUtils.jsonToMap(jsonStr);
		} catch (Exception e) {
			logger.error("查询出错！", e);
			jsonStr = e.getLocalizedMessage();
		}	
		if (map == null) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	

	/**
	 * 新增告警规则
	 * 
	 * @param alertRuleDTO
	 * @return
	 * @return CommonResult
	 * @author wupiao
	 * @since 2017年2月20日
	 */
	@RequestMapping(value = "/addAlertRule", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addAlertRule(@RequestBody K8sAlertDTO k8sAlertDTO) {
		Boolean isSuccess = false;
		try {
			alarmService.addAlertRule(k8sAlertDTO.getClusterCode(), k8sAlertDTO.getAlertRuleDTO());
			isSuccess = true;
		} catch (Exception e) {
			isSuccess = false;
			logger.error("addAlertRule出错！", e);
		}
		if (isSuccess) {
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1101, ReturnCodeDefine.Msg.M1200);
	}

	/**
	 * 修改告警规则
	 * 
	 * @param alertRuleDTO
	 * @return
	 * @return CommonResult
	 * @author wupiao
	 * @since 2017年2月20日
	 */
	@RequestMapping(value = "/modifyAlertRule", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult modifyAlertRule(@RequestBody K8sAlertDTO k8sAlertDTO) {
		Boolean isSuccess = false;
		try {
			alarmService.modifyAlertRule(k8sAlertDTO.getClusterCode(), k8sAlertDTO.getAlertRuleDTO());
			isSuccess = true;
		} catch (Exception e) {
			isSuccess = false;
			logger.error("modifyAlertRule出错！", e);
		}
		if (isSuccess) {
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1101, ReturnCodeDefine.Msg.M1200);
	}

	/**
	 * 删除告警规则
	 * 
	 * @param alertRuleDTO
	 * @return
	 * @return CommonResult
	 * @author wupiao
	 * @since 2017年2月20日
	 */
	@RequestMapping(value = "/deleteAlertRule", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult deleteAlertRule(@RequestBody K8sAlertDTO k8sAlertDTO) {
		Boolean isSuccess = false;
		try {
			alarmService.deleteAlertRule(k8sAlertDTO.getClusterCode(), k8sAlertDTO.getAlertRuleDTO());
			isSuccess = true;
		} catch (Exception e) {
			isSuccess = false;
			logger.error("deleteAlertRule出错！", e);
		}
		if (isSuccess) {
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1101, ReturnCodeDefine.Msg.M1200);
	}
}
